package com.project.Service.impl;


import com.project.Bean.Room;
import com.project.Service.IRoomService;
import com.project.mapper.IRoomFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 房间实现类
 */
public class IRoomServiceImpl extends BaseService implements IRoomService {

    @Override
    public void addRoom(Room room) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.addRoom(room);
        session.commit();
        session.close();
    }

    @Override
    public boolean delRoom(int id) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        IStudentMapper mapper1 = session.getMapper(IStudentMapper.class);
        IRoomFacilityMapper mapper2 = session.getMapper(IRoomFacilityMapper.class);
        if(mapper1.countByRoom(id) == 0){
            mapper2.delFacility(id);
            mapper.delRoom(id);
            session.commit();
            session.close();
        }
        return false;
    }

    @Override
    public Room findById(int id) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Room room = mapper.findById(id);
        session.close();
        return room;
    }

    @Override
    public List<Room> findAll(String address, String statues, String can, String type) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<Room> rooms = mapper.findAll(address, statues, can, type);
        session.close();
        return rooms;
    }
}
